#!/bin/bash


log_dir="/home/zuo/25_OA自动化下载/log"
backup_dir="/home/zuo/25_OA自动化下载/log_backup"  
mkdir -p "$backup_dir" 


yesterday=$(date -d "yesterday" "+%Y%m%d")


zip_file="$backup_dir/logs_$yesterday.zip"


log_files=$(find "$log_dir" -type f -name "*$yesterday*.log")


if [[ -z "$log_files" ]]; then
    echo "没有找到 $yesterday 的日志文件，无需备份。"
    exit 0
fi


echo "开始压缩 $yesterday 的日志文件..."
echo "$log_files" | zip -@ "$zip_file"


if [[ $? -eq 0 ]]; then
    echo "日志文件已成功压缩到 $zip_file"
    echo "开始删除已备份的日志文件..."

    echo "$log_files" | xargs rm -f
    if [[ $? -eq 0 ]]; then
        echo "前一天的日志文件已成功删除。"
    else
        echo "删除日志文件时发生错误，请检查。"
    fi
else
    echo "日志压缩失败，未删除日志文件。"
    exit 1
fi
